// 蓝牙数据指令
/**
 * 参数设置 0x02
 */
// function  set2(){
//   var cmds = new ArrayBuffer(6);
//   var dataview = new DataView(cmds);
//   dataview.setUint8(0, "0x68");
//   dataview.setUint8(1, "0x02");
//   dataview.setUint8(2, "0x00");
//   dataview.setUint8(3, "0x00");
//   dataview.setUint8(4, "0x01");
//   dataview.setUint8(5, "bianhao");
//   dataview.setUint8(6, "neirong");
//   dataview.setUint8(7, "0x00");
//   dataview.setUint8(8, "0x16");
//   dataview = setcrc8(dataview, 7);
//   return cmds;
// }


/***
 * 查看电量 0x03
 */
function set3() {
  var cmds = new ArrayBuffer(6);
  var dataview = new DataView(cmds);
  dataview.setUint8(0, "0x68");
  dataview.setUint8(1, "0x03");
  dataview.setUint8(2, "0x00");
  dataview.setUint8(3, "0x00");
  dataview.setUint8(4, "0x00");  
  dataview.setUint8(5, "0x16");
  dataview = setcrc8(dataview, 4);
  return cmds;
}
/*
*设置计步参数 0x04
*height 身高
*weight 体重
*sex 性别 0男 1女
*age 年龄
*/
function set4(height=178,weight=60,sex=0,age=28){
   var cmds=new ArrayBuffer(10);
   var dataview=new DataView(cmds);   
   dataview.setUint8(0, "0x68");
   dataview.setUint8(1,"0x04");
   dataview.setUint8(2,"0x04");
   dataview.setUint8(3, "0x00");   
   dataview.setUint8(4,dec2hex(height));
   dataview.setUint8(5, dec2hex(weight));
   dataview.setUint8(6, dec2hex(sex));
   dataview.setUint8(7, dec2hex(28));
   dataview.setUint8(8, "0x00");
   dataview.setUint8(9, "0x16");
   dataview=setcrc8(dataview, 8);
   return cmds;
}
/***
 * 获取手环检测时时数据 0x06
 */
//打开心率设置
function set6_1(){
  var cmds=new ArrayBuffer(7);
  var dataview=new DataView(cmds);
  dataview.setUint8(0,"0x68");
  dataview.setUint8(1,"0x06");
  dataview.setUint8(2, "0x01");
  dataview.setUint8(3, "0x00");
  dataview.setUint8(4, "0x01");
  dataview.setUint8(5, "0x00");
  dataview.setUint8(6, "0x16");
  dataview=setcrc8(dataview,5);
  return cmds;
}
//获取实时数据  0x06
function set6_2(){
  var cmds = new ArrayBuffer(7);
  var dataview = new DataView(cmds);
  dataview.setUint8(0, "0x68");
  dataview.setUint8(1, "0x06");
  dataview.setUint8(2, "0x01");
  dataview.setUint8(3, "0x00");   
  dataview.setUint8(4, "0x00");
  dataview.setUint8(5, "0x00");      
  dataview.setUint8(6, "0x16");   
  dataview=setcrc8(dataview,5);
  return cmds;
}
//关闭心率测试 0x06
function set6_3(){
  var cmds = new ArrayBuffer(7);
  var dataview = new DataView(cmds);
  dataview.setUint8(0, "0x68");
  dataview.setUint8(1, "0x06");
  dataview.setUint8(2, "0x01");
  dataview.setUint8(3, "0x00");
  dataview.setUint8(4, "0x02");
  dataview.setUint8(5, "0x00");
  dataview.setUint8(6, "0x16");
  dataview = setcrc8(dataview, 5);
  return cmds;
}
/**
 * 恢复出厂设置  Ox11
 */
function set11(){
  var cmds = new ArrayBuffer(7);
  var dataview = new DataView(cmds);
  dataview.setUint8(0, "0x68");
  dataview.setUint8(1, "0x11");
  dataview.setUint8(2, "0x01");
  dataview.setUint8(3, "0x00");
  dataview.setUint8(4, "0x01");
  dataview.setUint8(5, "0x00");
  dataview.setUint8(6, "0x16");
  dataview = setcrc8(dataview, 5);
  return cmds;
}
/**
 *找手环功能 开始
 */
function set13_1(){
  var cmds = new ArrayBuffer(7);
  var dataview = new DataView(cmds);
  dataview.setUint8(0, "0x68");
  dataview.setUint8(1, "0x13");
  dataview.setUint8(2, "0x01");
  dataview.setUint8(3, "0x00");
  dataview.setUint8(4, "0x00");
  dataview.setUint8(5, "0x00");
  dataview.setUint8(6, "0x16");
  dataview = setcrc8(dataview, 5);
  return cmds;
}
/**
 * 找手环 结束  ox13
 */
function set13_2() {
  var cmds = new ArrayBuffer(7);
  var dataview = new DataView(cmds);
  dataview.setUint8(0, "0x68");
  dataview.setUint8(1, "0x13");
  dataview.setUint8(2, "0x01");
  dataview.setUint8(3, "0x00");
  dataview.setUint8(4, "0x01");
  dataview.setUint8(5, "0x00");
  dataview.setUint8(6, "0x16");
  dataview = setcrc8(dataview, 5);
  return cmds;
}
/**
 * 设置时间  0x20
 */
function set20(){
   var hex_time = parseInt(new Date().getTime()/1000+60*60*8).toString(16);   
   var cmds = new ArrayBuffer(10);
   var dataview = new DataView(cmds);
   dataview.setUint8(0, "0x68");
   dataview.setUint8(1, "0x20");
   dataview.setUint8(2, "0x04");
   dataview.setUint8(3, "0x00");   
   dataview.setUint8(4, "0x" + hex_time.substr(6,2));
   dataview.setUint8(5, "0x" + hex_time.substr(4, 2));
   dataview.setUint8(6, "0x" + hex_time.substr(2, 2));
   dataview.setUint8(7, "0x" + hex_time.substr(0, 2));   
   dataview.setUint8(8, "0x00");
   dataview.setUint8(9, "0x16");
   dataview = setcrc8(dataview, 8);
   return cmds;
}
/**
 * 获取监听数据
 * 1 获取电量
 * 2 
 */
function gets(buffer){
  var dataView = new DataView(buffer);
  if (dataView.getUint8(1) == parseInt("83", 16)) {//获取电量
    console.log("获取当前电量" + dataView.getUint8(4));
     wx.setStorageSync("bracelet_data.electric", dataView.getUint8(4));     
  }else if (dataView.getUint8(1) == parseInt("86", 16)) {
    if (dataView.getUint8(4)==0){//获取数据
      //"当前心率：bpm" 
      var p1 = dataView.getUint8(5);
      wx.setStorageSync("bracelet_data.current_heart_rate",p1);
      console.log("当前心率:"+p1);
      //"当前步伐：pace 步"
      var p2 =(dataView.getUint8(6) * Math.pow(16, 0) + dataView.getUint8(7) * Math.pow(16, 2) + dataView.getUint8(8) * Math.pow(16, 4) + dataView.getUint8(9) * Math.pow(16, 6));
      wx.setStorageSync("bracelet_data.current_pace", p2);
      console.log("当前步伐:" + p2);
      //"当前里程: mileage 米"
      var p3 =(dataView.getUint8(10) * Math.pow(16, 0) + dataView.getUint8(11) * Math.pow(16, 2) + dataView.getUint8(12) * Math.pow(16, 4) + dataView.getUint8(13) * Math.pow(16, 6));
      wx.setStorageSync("bracelet_data.current_mileage", p3);
      console.log("当前里程:" + p3);
      //当前卡路里calorie  kcal
      var p4 = ":" + (dataView.getUint8(14) * Math.pow(16, 0) + dataView.getUint8(15) * Math.pow(16, 2) + dataView.getUint8(16) * Math.pow(16, 4) + dataView.getUint8(17) * Math.pow(16, 6));
      wx.setStorageSync("bracelet_data.current_calorie", p4);
      console.log("当前卡路里:" + p3);
      var result = p1 + "\n" + p2 + "\n" + p3 + "\n" + p4;      
    } else if(dataView.getUint8(4)==1){
        console.log("打开心率");
    }else if(dataView.getUint8(4)==2){
        console.log("关闭心率");
    }
          
  }else if (dataView.getUint8(1) == parseInt("a6", 16)) {
      var datatype = dataView.getUint8(7);
      var t1 = dataView.getUint8(4);
      var t2 = dataView.getUint8(5);
      var t3 = dataView.getUint8(6);
      if (datatype == 0) {//全天总数据
        console.log("全天总数据");
        blue_write(replay_set0(t1,t2,t3));
      } else if (datatype == 1) {//全天运动情况
        console.log("全天运动情况");
        blue_write(replay_set1(t1, t2, t3));
      } else if (datatype == 2) {//全天睡眠情况
        console.log("全天睡眠情况");
        blue_write(replay_set2(t1, t2, t3));
      } else if (datatype == 3) {//全天心率
        console.log("全天心率");
        blue_write(replay_set3(t1, t2, t3));
      } else if (datatype == 4) {//计步详情
        console.log("计步详情");
        blue_write(replay_set4(t1, t2, t3));
      } else if (datatype == 5) {//热量详情
        console.log("热量详情");
        blue_write(replay_set5(t1, t2, t3));
      } else if (datatype == 6) {//里程详情
        console.log("里程详情");
        blue_write(replay_set6(t1, t2, t3));
      }
    }
  }
/////////////////////app 应答数据/////////////////////////////////////
function blue_write(cmd){
  var charset_write = getApp().globalData.charset_write[0];
  var charset_notify = getApp().globalData.charset_notify[0];
  var deviceId = getApp().globalData.current_con_deviceId;
  var serviceId = getApp().globalData.current_con_serviceId[0];
  //管道写值
  wx.writeBLECharacteristicValue({
    deviceId: deviceId,
    serviceId: serviceId,
    characteristicId: charset_write,
    value: cmd,
    success: function (res) {
      console.log("发送指令成功1(app应答)：", res);
    },
    fail: function (res) { console.log("发送指令失败(app应答):", res); }
  })
}
/**
 * 全天总数据 app应答 0x26
 * t1,t2,t3  手环的年月日
 */
function  replay_set0(t1,t2,t3){
  var cmds = new ArrayBuffer(10);
  var dataview = new DataView(cmds);
  dataview.setUint8(0, "0x68");
  dataview.setUint8(1, "0x26");
  dataview.setUint8(2, "0x04");
  dataview.setUint8(3, "0x00");
  dataview.setUint8(4, dec2hex(t1));
  dataview.setUint8(5, dec2hex(t2));
  dataview.setUint8(6, dec2hex(t3));
  dataview.setUint8(7, "0x00");
  dataview.setUint8(8, "0x00");
  dataview.setUint8(9, "0x16");
  dataview = setcrc8(dataview, 8);
  return cmds;
}

/**
 * 全天运动数据 app应答 0x26
 * t1,t2,t3  手环的年月日
 */
function replay_set1(t1, t2, t3) {
  var cmds = new ArrayBuffer(10);
  var dataview = new DataView(cmds);
  dataview.setUint8(0, "0x68");
  dataview.setUint8(1, "0x26");
  dataview.setUint8(2, "0x04");
  dataview.setUint8(3, "0x00");
  dataview.setUint8(4, dec2hex(t1));
  dataview.setUint8(5, dec2hex(t2));
  dataview.setUint8(6, dec2hex(t3));
  dataview.setUint8(7, "0x01");
  dataview.setUint8(8, "0x00");
  dataview.setUint8(9, "0x16");
  dataview = setcrc8(dataview, 8);
  return cmds;
}

/**
 * 全天睡眠数据 app应答 0x26
 * t1,t2,t3  手环的年月日
 */
function replay_set2(t1, t2, t3) {
  var cmds = new ArrayBuffer(10);
  var dataview = new DataView(cmds);
  dataview.setUint8(0, "0x68");
  dataview.setUint8(1, "0x26");
  dataview.setUint8(2, "0x04");
  dataview.setUint8(3, "0x00");
  dataview.setUint8(4, dec2hex(t1));
  dataview.setUint8(5, dec2hex(t2));
  dataview.setUint8(6, dec2hex(t3));
  dataview.setUint8(7, "0x02");
  dataview.setUint8(8, "0x00");
  dataview.setUint8(9, "0x16");
  dataview = setcrc8(dataview, 8);
  return cmds;
}

/**
 * 全天心率数据 app应答 0x26
 * t1,t2,t3  手环的年月日
 */
function replay_set3(t1, t2, t3) {
  var cmds = new ArrayBuffer(10);
  var dataview = new DataView(cmds);
  dataview.setUint8(0, "0x68");
  dataview.setUint8(1, "0x26");
  dataview.setUint8(2, "0x04");
  dataview.setUint8(3, "0x00");
  dataview.setUint8(4, dec2hex(t1));
  dataview.setUint8(5, dec2hex(t2));
  dataview.setUint8(6, dec2hex(t3));
  dataview.setUint8(7, "0x03");
  dataview.setUint8(8, "0x00");
  dataview.setUint8(9, "0x16");
  dataview = setcrc8(dataview, 8);
  return cmds;
}

/**
 * 计步详情数据 app应答 0x26
 * t1,t2,t3  手环的年月日
 */
function replay_set4(t1, t2, t3) {
  var cmds = new ArrayBuffer(10);
  var dataview = new DataView(cmds);
  dataview.setUint8(0, "0x68");
  dataview.setUint8(1, "0x26");
  dataview.setUint8(2, "0x04");
  dataview.setUint8(3, "0x00");
  dataview.setUint8(4, dec2hex(t1));
  dataview.setUint8(5, dec2hex(t2));
  dataview.setUint8(6, dec2hex(t3));
  dataview.setUint8(7, "0x04");
  dataview.setUint8(8, "0x00");
  dataview.setUint8(9, "0x16");
  dataview = setcrc8(dataview, 8);
  return cmds;
}

/**
 * 热量详情数据 app应答 0x26
 * t1,t2,t3  手环的年月日
 */
function replay_set5(t1, t2, t3) {
  var cmds = new ArrayBuffer(10);
  var dataview = new DataView(cmds);
  dataview.setUint8(0, "0x68");
  dataview.setUint8(1, "0x26");
  dataview.setUint8(2, "0x04");
  dataview.setUint8(3, "0x00");
  dataview.setUint8(4, dec2hex(t1));
  dataview.setUint8(5, dec2hex(t2));
  dataview.setUint8(6, dec2hex(t3));
  dataview.setUint8(7, "0x05");
  dataview.setUint8(8, "0x00");
  dataview.setUint8(9, "0x16");
  dataview = setcrc8(dataview, 8);
  return cmds;
}

/**
 * 里程详情数据 app应答 0x26
 * t1,t2,t3  手环的年月日
 */
function replay_set6(t1, t2, t3) {
  var cmds = new ArrayBuffer(10);
  var dataview = new DataView(cmds);
  dataview.setUint8(0, "0x68");
  dataview.setUint8(1, "0x26");
  dataview.setUint8(2, "0x04");
  dataview.setUint8(3, "0x00");
  dataview.setUint8(4, dec2hex(t1));
  dataview.setUint8(5, dec2hex(t2));
  dataview.setUint8(6, dec2hex(t3));
  dataview.setUint8(7, "0x06");
  dataview.setUint8(8, "0x00");
  dataview.setUint8(9, "0x16");
  dataview = setcrc8(dataview, 8);
  return cmds;
}

//////////////////////////////////////////////////////////
/***
 * 10进制转16进制 
 * param 必须是 int 类型
 */
function dec2hex(param){
  var hex = param.toString(16);  
  if(hex.length==1){        
     return "0x0"+hex;
  }else{  
    hex=hex.substr(-2);
    return "0x" + hex;
  }
}
/** * 
 * 16进制10进制
 * param 必须是字符串
 */
function hex2dec(param){
  try{
    if (param.startsWith("0x") || param.startsWith("0X")) {
      return parseInt(param);
    } else {
      return parseInt(param, 16);
    }
  }catch(e){
    wx.showToast({
      title: '参数转换错误',
    })
  }  
}
/***
 * 设置crc8
 * dataview 数据
 * offset cr数据迁移位
 */
function setcrc8(dataview,offset){
  var dec = 0; 
  try{
    for (var i = 0; i < offset; i++) {
      dec += dataview.getUint8(i);
    }
    dataview.setUint8(offset, dec2hex(dec));
  }catch(e){
        console.log(e);
  }  
  return dataview;
}

module.exports = {
  set4: set4,  
  set6_1: set6_1,
  set6_2: set6_2,
  set6_3: set6_3,
  set13_1: set13_1,
  set13_2: set13_2,
  set20: set20,
  set11,set11,
  set3: set3,
  gets: gets
}